import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import Layout from "@/parts/html/layout/index";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "index",
        name: "yjIndex",
        component: () =>
          import(/* webpackChunkName: "about" */ "@/views/Home.vue")
      }
    ]
  },
  {
    path: "/login",
    name: "login",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ "@/views/login.vue")
  },
  {
    path: "/info",
    name: "info",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ "@/views/info.vue")
  },
  {
    path: "/404",
    name: "404",
    component: () => import("@/views/error/404")
  },
  {
    path: "*",
    name: "emptyPage",
    component: () => import("@/views/error/404")
  }
];

const router = new VueRouter({
  // mode: "history",
  base: process.env.BASE_URL,
  routes
});

// 全局守卫判断
let aNoLoginPages = ["/login", "/", "/index"];

router.beforeEach((to, from, next) => {
  // 判断是否登陆
  if (store.state.sid && store.state.sid.access_token) {
    // 判断当前用户是否已拉取拉取用户信息
    // if (!store.state.userData) {
    // if (!store.state.sid) {
    // } else {
    //   if (to.path === "/login") {
    //     next({ path: "/info" });
    //   } else {
    //     next(); //当有用户权限的时候，说明所有可访问路由已生成 如访问没权限的全面会自动进入404页面
    //   }
    // }
    if (to.path === "/login") {
      next({ path: "/info" });
    } else {
      next(); //当有用户权限的时候，说明所有可访问路由已生成 如访问没权限的全面会自动进入404页面
    }
  } else {
    if (aNoLoginPages.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next();
    } else {
      next("/login"); // 否则全部重定向到登录页
    }
  }
});
export default router;
